//
//  UIButton+Extension.swift
//  HotNews
//
//  Created by yang on 2017/9/23.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

import Foundation

extension UIButton
{
    /// 创建图片按钮
    ///
    /// - Parameters:
    ///   - imageStr: 图片名称
    ///   - backgroundImageStr: 背景图片名称
    ///   - target: 目标
    ///   - action: 事件
    convenience init(imageStr: String, backgroundImageStr: String?, target: Any?, action: Selector)
    {
        self.init()
        
        setImage(UIImage(named: imageStr), for: .normal)
        if let backgroundImageStr = backgroundImageStr
        {
            setBackgroundImage(UIImage(named: backgroundImageStr), for: .normal)
        }
        addTarget(target, action: action, for: .touchUpInside)
        sizeToFit()
    }
    
    
    /// 创建文本按钮
    ///
    /// - Parameters:
    ///   - title: 标题
    ///   - fontSize: 字体大小
    ///   - normalColor: 默认颜色
    ///   - highlightedColor: 高亮颜色
    ///   - backgroundImageName: 背景图片
    convenience init(title: String, fontSize: CGFloat, normalColor: UIColor, highlightedColor: UIColor = UIColor.RGBColor(red: 20, green: 82, blue: 184), backgroundImageName: String?)
    {
        self.init()
        setTitle(title, for: .normal)
        titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        setTitleColor(normalColor, for: .normal)
        setTitleColor(highlightedColor, for: .highlighted)
    
        if let backgroundImageName = backgroundImageName
        {
            //设置背景图片
            setBackgroundImage(UIImage(named: backgroundImageName), for: .normal)
        }
        sizeToFit()
    }
    /// 创建文本按钮(有触发事件)
    ///
    /// - Parameters:
    ///   - title: 标题文字
    ///   - fontSize: 字体大小
    ///   - normalColor: 默认颜色
    ///   - highlightedColor: 高亮颜色
    ///   - target: 目标
    ///   - action: 事件
    ///   - backgroundImageName: 背景图片
    convenience init(title: String, fontSize: CGFloat, normalColor: UIColor, highlightedColor: UIColor = UIColor.RGBColor(red: 20, green: 82, blue: 184), target: Any?, action: Selector, backgroundImageName: String?)
    {
        self.init()
        setTitle(title, for: .normal)
        titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        setTitleColor(normalColor, for: .normal)
        setTitleColor(highlightedColor, for: .highlighted)
        addTarget(target, action: action, for: .touchUpInside)

        if let backgroundImageName = backgroundImageName
        {
            //设置背景图片
            setBackgroundImage(UIImage(named: backgroundImageName), for: .normal)
        }
        sizeToFit()
    }
    
}
